package l1j.opqlo.NewSystem.LABA;


import static l1j.server.server.model.skill.L1SkillId.Laba_DELAY;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.sql.Timestamp;
import java.util.Calendar;
import java.util.TimeZone;
import java.util.logging.Level;
import java.util.logging.Logger;

import l1j.opqlo.NewSystem.A_ColorMessage;
import l1j.opqlo.Server.L1World.L1World;
import l1j.server.Config;
import l1j.server.server.datatables.ItemTable;
import l1j.server.server.model.L1Inventory;
import l1j.server.server.model.Instance.L1ItemInstance;
import l1j.server.server.model.Instance.L1PcInstance;
import l1j.server.server.serverpackets.S_EffectLocation;
import l1j.server.server.serverpackets.S_SystemMessage;
import l1j.server.server.utils.Random;
import l1j.server.server.utils.StreamUtil;

public class L1LaBa {


	private L1PcInstance _pc ;


	private static int _id;
	/**拉霸總次數*/
	private static int _round;
	/**中獎次數*/
	private static int _hit;

	/**沒中次數*/
	private static int _miss;

	/**總彩金累計*/
	public static int _totalprize;
	/**獲得大獎的玩家名稱*/
	private static String _WinnerName;
	private static final Logger _log = Logger.getLogger(L1LaBa.class.getName());


	private static void addhit(){
		_hit++;
	}

	private static void addmiss(){
		_miss++;
	}
	private static void addround(){
		_round++;
		if(_round % 100 ==0){
			LabaTable.getInstance().setLabaData();
		}
	}
	private static void addtotalprize(final int i){
		_totalprize+=i;
	}

	public static int gethit(){
		return _hit;
	}

	/**第幾場拉霸*/
	public static int getid(){
		if(_id<1){_id=1;}
		return _id;
	}
	public static int getmiss(){
		return _miss;
	}
	public static int getround(){
		return _round;
	}

	public static int gettotalprize(){
		if(_totalprize <8888){
			_totalprize=8888;
		}
		return _totalprize;
	}

	public static String getWinnerName(){
		return _WinnerName;
	}
	
	private static void LabaSave(final String info) {
		BufferedWriter out = null;
		try {
			final TimeZone tz = TimeZone.getTimeZone(Config.TIME_ZONE);
			final Calendar cal = Calendar.getInstance(tz);
			final String	time = new StringBuilder().append(cal.get(Calendar.YEAR)-2000).append(cal.get(Calendar.MONTH)+1).append(cal.get(Calendar.DATE)).toString();
			out = new BufferedWriter(new FileWriter(Config.LOG_LOCATION+"記錄/海賊拉霸/拉霸"+time+".txt", true));
			out.write(info + "\r\n");
		} catch (final IOException e) {
			_log.log(Level.SEVERE , e.getLocalizedMessage() , e);
		}finally{
			StreamUtil.close(out);
		}
	}
	public static void sethit(final int i){
		_hit=i;
	}

	public static void setid(final int i){
		_id=i;
	}
	public static void setmiss(final int i){
		_miss=i;
	}
	public static void setround(final int i){
		_round=i;
	}
	public static void settotalprize(final int i){
		_totalprize = i;
	}

	public static void setWinnerName(final String pcname){
		_WinnerName = pcname;
	}

	public void addid(){
		_id++;
	}
	private  boolean createNewItem(final int item_id,final int count) {
		final L1ItemInstance item = ItemTable.getInstance().createItem(item_id);
		if (item != null) {
			item.setCount(count);
			if (this._pc.getInventory().checkAddItem(item, count) == L1Inventory.OK) {
				this._pc.getInventory().storeItem(item);
			} else {
				this._pc.sendPackets(new S_SystemMessage("身上負重導致物品贈送失敗！。"));
				return false;
			}
			this._pc.sendPackets(new S_SystemMessage("恭喜拉霸中獎！獲得 "+item.getLogName()+"。"));
			return true;
		} else {
			return false;
		}
	}

	private void LabaMeed(int a, int b, int c){//opqlo 拉霸獎品
		addround();//記錄次數+1
		final int Datum= 6410;
		a-=Datum;
		b-=Datum;
		c-=Datum;
		String msg =null;
		String bmsg=null;
		final String name = this._pc.getName();
		//		if(a==7&&b==a&&c==a){//7號 魯夫三條
		//			this.createNewItem(24876,1);//【魯夫帽】
		//			addtotalprize(1);
		//			addhit();
		//			bmsg="玩家： "+name+"拉霸 出現【魯夫】三條....";
		//		}else if(a==8&&b==a&&c==a){//8號 喬巴三條
		//			this.createNewItem(24875,1);//【喬巴帽】
		//			addtotalprize(1);
		//			addhit();
		//			bmsg="玩家： "+name+"拉霸 出現【喬巴】三條....";
		//		}else if(a==3&&b==a&&c==a){ //3號 娜美三條
		//			this.createNewItem(24874,1);//【天候棒】
		//			addtotalprize(1);
		//			addhit();
		//			bmsg="玩家： "+name+"拉霸 出現【娜美】三條....";
		//		}else if(a==2&&b==a&&c==a){//2號 騙人布三條
		//			this.createNewItem(24873,1);//【兜】
		//			addtotalprize(1);
		//			addhit();
		//			bmsg="玩家： "+name+"拉霸 出現【騙人布】三條....";
		//		}else if(a==6&&b==a&&c==a){//6號 索隆三條
		//			this.createNewItem(24872,1);//【三刀流】
		//			addtotalprize(1);
		//			addhit();
		//			bmsg="玩家： "+name+"拉霸 出現【索隆】三條....";
		//
		//		}else 
		if(a==10&&b==a&&c==a){//10號 女帝三條
			setWinnerName(name);//記錄 中獎者名稱
			this.createNewItem(90308,gettotalprize());//商城金幣
			bmsg="玩家： 《"+name+"》 拉霸中頭獎拉下彩金 商城元寶("+gettotalprize()+") 。";
			LabaTable.getInstance().setLabaData();
			this.addid();//場次編號+1
			LabaTable.getInstance().newLabaData();
		}else{
			if(a==b&&b==c&&c==a){//其他類型三條
				addhit();
				this.createNewItem(90308,1888);
				bmsg="玩家： "+name+"拉霸 出現【三條】!!";

			}else if(a==b||b==c||c==a){//其他類型一對
				addhit();
				this.createNewItem(90308,188);
				msg="恭喜你...拉霸出現【一對】!!";
			}else{
				addmiss();
				msg="喔喔...請次下次再努力囉。";
			}
		}
		addtotalprize(4);
		if(msg!=null){
			this._pc.sendPackets(new S_SystemMessage(msg));
		}
		if(bmsg!=null){
			L1World.getInstance().broadcastPacketToAll(new A_ColorMessage(15,bmsg));
			LabaSave(new StringBuilder().append(new Timestamp(System.currentTimeMillis())).append("	").append(bmsg).toString());
		}
		this._pc.setSkillEffect(Laba_DELAY, 1000);//延遲一秒才能再啟動拉霸
	}

	public  void LabaStart(final L1PcInstance pc){
		this._pc =pc;

		final int effect=6411;

		int Effect1 =Random.nextInt(10)+effect;
		int Effect2 =Random.nextInt(10)+effect;
		int Effect3 =Random.nextInt(10)+effect;
		boolean isRe = (Effect1 == Effect2) || (Effect1 == Effect3) || (Effect2 == Effect3);

		while(isRe){
			if(Effect1==Effect2&&Effect3==Effect1){
				final int chance = Effect1==6420? 95 : 30;
				if(Random.nextInt(100)+1 >chance){
					isRe = false; break;
				}
			}else if((Effect1 == Effect2) || (Effect1 == Effect3) || (Effect2 == Effect3)){
				if(Random.nextInt(100)+1 >25){
					isRe = false; break;
				}
			}else{
				isRe = false; break;
			}
			Effect1 =Random.nextInt(10)+effect;
			Effect2 =Random.nextInt(10)+effect;
			Effect3 =Random.nextInt(10)+effect;
		}

		//		if(Effect1==Effect2&&Effect3==Effect1){
		//			if(!this._pc.isLabaReset()){
		//				int chance =0;
		//				if(Effect1==6420){
		//					chance =70;
		//				}else{
		//					chance =30;
		//				}
		//				if(Random.nextInt(100)+1<=chance){
		//					this.LabaStart(this._pc);
		//					this._pc.setLabaReset(true);//中大獎必須重拉一次
		//					return;
		//				}
		//			}
		//		}
		this.LabaMeed(Effect1,Effect2,Effect3);

		final int locx =this._pc.getX();
		final int locy =this._pc.getY();
		final S_EffectLocation packet1 = new S_EffectLocation(locx-2, locy-2, Effect1);
		this._pc.sendPackets(packet1);

		final S_EffectLocation packet2 = new S_EffectLocation(locx, locy, Effect2);
		this._pc.sendPackets(packet2);

		final S_EffectLocation packet3 = new S_EffectLocation(locx+2, locy+2, Effect3);
		this._pc.sendPackets(packet3);

		/*if(Effect1==Effect2&&Effect3==Effect1){
			_pc.broadcastPacket(packet1);//opqlo 展示用
			_pc.broadcastPacket(packet2);//opqlo 展示用
			_pc.broadcastPacket(packet3);//opqlo 展示用
		}*/
	}

	//	int big =0 , thred=0 , pair=0 , miss =0;
	//	int win =0 , lose=0 , bonus =0; 
	//	for(int i=0; i<10000; i++){
	//		win+=96;
	//		bonus+=4;
	//		int a =Random.nextInt(10)+1;
	//		int b =Random.nextInt(10)+1;
	//		int c =Random.nextInt(10)+1;
	//		boolean isRe = (a == b) || (a == c) || (b == c);
	//
	//		while(isRe){
	//			if(a==b&&c==a){
	//				final int chance = a==10? 95 : 30;
	//				if(Random.nextInt(100)+1 >chance){
	//					isRe = false; break;
	//				}
	//			}else if((a == b) || (a == c) || (b == c)){
	//				if(Random.nextInt(100)+1 >25){
	//					isRe = false; break;
	//				}
	//			}else{
	//				isRe = false; break;
	//			}
	//		}
	//
	//		if(a==10&&b==a&&c==a){//10號 女帝三條
	//			big++;
	//			lose += bonus + 8888;
	//			bonus =0;
	//		}else{
	//			if(a==b&&b==c&&c==a){//其他類型三條
	//				thred++;
	//				lose += 1888;
	//			}else if(a==b||b==c||c==a){//其他類型一對
	//				pair++;
	//				lose +=188;
	//			}else{
	//				miss++;
	//			}
	//		}
	//	}
	//	System.out.println("彩金:"+big +" 三條:"+thred +" 一對:"+pair+" 槓龜:"+miss);
	//	System.out.println("收入:"+(bonus+win)+"支出:"+lose);
	//	if(miss >0){
	//		Thread.sleep(5000);
	//		return;
	//	}

}
